import SimpleFlow from "./impl/SimpleFlow"
import TestFlow from "./impl/TestFlow"

import flowGroup from "./flowGroup"
import FlowApp from "./core/FlowApp"
import FlowInfo from "./core/FlowInfo"
import FlowLine from "./core/FlowLine"

import JsonSeriType from "@/lib/field/base/JsonSeriType"
import classLoader from "@/lib/field/core/classLoader"


const DEFAULT_FLOW_PAGE = "_flow";

const FLOW_NODE_CONSTANTS = {
	nodeWare:{},
	nodeList:[],
	creatorWare:{},
};
function load(flowNode,creator){
	FLOW_NODE_CONSTANTS.nodeWare[flowNode.flowType] = flowNode;
	FLOW_NODE_CONSTANTS.creatorWare[flowNode.flowType] = creator;
	FLOW_NODE_CONSTANTS.nodeList.push(flowNode);
	loadClass(flowNode,creator);
}
function loadClass(flowNode,creator){
	if(creator !=null){
		let cobj = creator();
		if(cobj != null && cobj.seriType != null){
			classLoader.loadClass(cobj.seriType.type,flowNode,creator,cobj.seriType.page);
		}else{
			loadClass(flowNode.flowType,flowNode,creator,DEFAULT_FLOW_PAGE);
		}
	}
}
function loadAll(){
	load(SimpleFlow,function(params){
		return new SimpleFlow(params);
	});
	load(TestFlow,function(params){
		return new TestFlow(params);
	});
	
	loadClass(FlowApp,function(params){
		return new FlowApp(params);
	});
	
	loadClass(FlowInfo,function(params){
		return new FlowInfo(params);
	});
	
	loadClass(FlowLine,function(params){
		return new FlowLine(params);
	});
	
	classLoader.loadClass(JsonSeriType._seriType.type,JsonSeriType,function(params){
		return new JsonSeriType(params);
	},JsonSeriType._seriType.page);
	
}
function getAllFlow(){
	return FLOW_NODE_CONSTANTS.nodeList;
}

function getAllGroupFlow(){
	return flowGroup.groupAll(getAllFlow());
}

function getFlowGen(flowType){
	return FLOW_NODE_CONSTANTS.nodeWare[flowType];
}

function createFlowUUid(type){
	return "type."+type+"." + Math.random().toString(36).substr(3, 10)
	
}

function getCreator(type, def){
	return FLOW_NODE_CONSTANTS.creatorWare[type] || def;
}

function createFlowNode(type){
	return getCreator(type)(createFlowUUid(type));
}

function createFlowLine(from, to){
	return new FlowLine(from, to);
}

function createApp(flowInfo, nodeList, lineList,options){
	return new FlowApp(flowInfo, nodeList, lineList,options);
}
function createFlowInfo(name){
	return new FlowInfo(name);
}
export default {
	getAllGroupFlow,
	getAllFlow,
	loadAll,
	load,
	getFlowGen,
	createFlowUUid,
	createFlowNode,
	createApp,
	createFlowInfo,
	createFlowLine
	
};